home *** CD-ROM | disk | FTP | other *** search
/ BBS in a Box 5 / BBS in a Box -Volume V (BBS in a Box) (April 1992).iso / Files / Bus / M / mathsubs.cpt / SIMUL.C < prev    next >
Encoding:
C/C++ Source or Header  |  1988-01-17  |  2.9 KB  |  79 lines  |  [TEXT/pZIP]

  1. #define    N    10
  2.  
  3. /*****************************************************************************
  4.  *                    simul()                     *
  5.  *****************************************************************************
  6.  * DESCRIPTION:    A function to solve a system of simultaneous linear         *
  7.  *        equations by the Gauss-Jordan elimination method.         *
  8.  *                                         *
  9.  * SYNOPSIS:    simul(m_in, m_out, n);                         *
  10.  *        int    n;        How many equations             *
  11.  *        double    *m_in;        Points to a (N by N+1) matrix of     *
  12.  *                     coefficients.  (N is #defined at    *
  13.  *                     compile time and must be the same   *
  14.  *                     for both this function and its         *
  15.  *                     caller.  simul() destroys m_in.     *
  16.  *        double    *m_out;        Points to (N by 1) array where         *
  17.  *                     result will be placed.             *
  18.  *                                         *
  19.  * EXAMPLE:    To solve the system:                         *
  20.  *                                         *
  21.  *             3x + 5y -  z =  3                     *
  22.  *            -2x - 3y + 4z =  8                     *
  23.  *              x      - 2z = -4                     *
  24.  *                                         *
  25.  *        Set n to 3 and then fill in the upper-left corner of m_in    *
  26.  *        with:                                 *
  27.  *                                         *
  28.  *              3   5  -1   3                         *
  29.  *             -2  -3   4   8                         *
  30.  *              1   0  -2  -4                         *
  31.  *                                         *
  32.  *        The result will then appear in the first three elements of   *
  33.  *        m_out.                                 *
  34.  *                                         *
  35.  * REVISIONS:    19 DEC 88 - RAC - Original code                     *
  36.  *****************************************************************************/
  37.  
  38. simul(m_in, m_out, n)
  39. double    m_in[N][N+1], m_out[N];
  40. int    n;
  41. {
  42.     int        top_row;            /* Row we're working on */
  43.     int        row;                /* Generic row index */
  44.     int        col;                /* Generic column index */
  45.     double    temp;
  46.  
  47.     for (top_row=0; top_row<n; top_row++) {    /* For each row ... */
  48.     for (row=top_row; row<n; row++)        /* Scan from current row to */
  49.         if (m_in[row][top_row] != 0.0)    /*  last row for a non-zero */
  50.         break;                /*  entry the "rowth" column */
  51.     if (m_in[row][top_row] != 0.0) {    /* If such a row found ... */
  52.         if (row != top_row) {        /*  and it's not the top one */
  53.         for (col=0; col<=n; col++) {    /*  swap with the top one */
  54.             temp = m_in[top_row][col];
  55.             m_in[top_row][col] = m_in[row][col];
  56.             m_in[row][col] = temp;
  57.             }                /* End swap */
  58.         }                /* End 'it's not the top' */
  59.         temp = m_in[top_row][top_row];    /* Normalize current row */
  60.         for (col=top_row; col<=n; col++)
  61.         m_in[top_row][col] /= temp;
  62.         for (row=top_row+1; row<n; row++) {    /* Fix up rows below */
  63.         temp = m_in[row][top_row] /
  64.                m_in[top_row][top_row];
  65.         for (col=top_row; col<=n; col++) {
  66.             m_in[row][col] -=
  67.                 temp * m_in[top_row][col];
  68.             }        
  69.         }                /* End 'fix up rows below' */
  70.         }                    /* End 'such a row found' */
  71.     }                    /* End 'for each row' */
  72.     for (row = n-1; row >= 0; row--) {        /* For each row (again) */
  73.     m_out[row] = m_in[row][n];        /* Figure out the answers! */
  74.     for (col=row+1; col<n; col++)
  75.         m_out[row] -=
  76.         m_in[row][col] * m_out[col];
  77.     }                    /* End 'for each row' */
  78.     }                        /* End simul() */
  79.